package com.example.lxc415billiards.controller;

import com.example.lxc415billiards.domain.Banner;
import com.example.lxc415billiards.service.BannerService;
import com.example.lxc415billiards.untils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/banner")
@Api(tags = "轮播图管理-李效丞0415")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @ApiOperation("获取所有Banner信息")
    @GetMapping("/getAllBanners")
    public AjaxResult getAllBanners() {
        List<Banner> banners = bannerService.getAllBanners();
        return AjaxResult.success(banners);
    }

    @ApiOperation("根据ID获取Banner信息")
    @GetMapping("/getBannerById/{id}")
    public AjaxResult getBannerById(@PathVariable int id) {
        Banner banner = bannerService.getBannerById(id);
        return banner != null ? AjaxResult.success(banner) : AjaxResult.error("Banner不存在");
    }

    @ApiOperation("新增Banner")
    @PostMapping("/addBanner")
    public AjaxResult addBanner(@RequestBody Banner banner) {
        Banner newBanner = bannerService.addBanner(banner);
        return AjaxResult.success(newBanner);
    }

    @ApiOperation("根据ID删除Banner")
    @DeleteMapping("/deleteBannerById/{id}")
    public AjaxResult deleteBannerById(@PathVariable int id) {
        bannerService.deleteBannerById(id);
        return AjaxResult.success("删除成功");
    }

    @ApiOperation("修改Banner信息")
    @PutMapping("/updateBanner")
    public AjaxResult updateBanner(@RequestBody Banner banner) {
        Banner updatedBanner = bannerService.updateBanner(banner);
        return updatedBanner != null ? AjaxResult.success(updatedBanner) : AjaxResult.error("更新失败");
    }
}
